package com.zoweunion.mechanic.controller.base;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.base.UpkeepService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;

/**
 * 保养套餐控制层
 * @author Administrator
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/upkeep")
public class UpkeepController extends BaseController{
	
	@Autowired
	private UpkeepService upkeepService;

	/**
	 * 查询保养时长
	 * @param str {o_id：车主id，品牌：brand ,车辆类型：car_type,车辆型号 model, o_id }
	 * @param request
	 * @return
	 * @throws MyException
	 */
	@PostMapping("/getUpkeepDuration")
	public ResponseBean getUpkeepDuration(@RequestBody String str,HttpServletRequest request) throws MyException{
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		logBefore(logger, "保养套餐_查询保养时长");
		Integer resultMap = upkeepService.getUpkeepDuration(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 查询保养时长
	 * @param str {o_id：车主id，品牌：brand ,车辆类型：car_type,车辆型号 model, o_id }
	 * @param request
	 * @return
	 * @throws MyException
	 */
	@PostMapping("/getUpkeepDurationNew")
	public ResponseBean getUpkeepDurationNew(@RequestBody String str,HttpServletRequest request) throws MyException{
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		logBefore(logger, "保养套餐_查询保养时长");
		Integer resultMap = upkeepService.getUpkeepDurationNew(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 根据保养id获取保养详细信息和相关配件信息
	 */
	@PostMapping("/getUpkeepInfo")
	public ResponseBean getUpkeepInfo(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_获取详情");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String, Object> resultMap = upkeepService.getUpkeepInfo(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 根据保养order_id获取保养详细信息和相关配件信息
	 */
	@PostMapping("/getUpkeepInfoByOrderId")
	public ResponseBean getUpkeepInfoByOrderId(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_获取详情");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String, Object> resultMap=upkeepService.getUpkeepInfoByOrderId(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}
	
	/**
	 * 新增保养套餐
	 */
	@PostMapping("/addUpkeep")
	public ResponseBean addUpkeep(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_新增保养套餐");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("update_user", getCurrentUser(request).getId());
		reqMap.put("create_user", getCurrentUser(request).getId());
		int result = upkeepService.addUpkeep(reqMap);
		logAfter(logger);
		if(result <= 0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"删除失败",null);
		}
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
	
	/**
	 * 删除保养套餐
	 */
	@PostMapping("/delete")
	public ResponseBean delete(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_删除保养套餐");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("update_user", getCurrentUser(request).getS_id());
		int result = upkeepService.delete(reqMap);
		logAfter(logger);
		if(result<=0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"删除失败",null);
		}
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
	
	/**
	 * 删除保养套餐
	 */
	@PostMapping("/deleteByCarInfo")
	public ResponseBean deleteByCarInfo(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_删除保养套餐");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("update_user", getCurrentUser(request).getS_id());
		int result = upkeepService.deleteByCarInfo(reqMap);
		logAfter(logger);
		if(result <= 0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"删除失败",null);
		}
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}

	/**
	 * 编辑保养套餐
	 */
	@PostMapping("/editUpkeep")
	public ResponseBean editUpkeep(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_编辑保养套餐");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("update_user", getCurrentUser(request).getId());
		reqMap.put("create_user", getCurrentUser(request).getId());
		int result = upkeepService.editUpkeep(reqMap);
		logAfter(logger);
		if(result <= 0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"删除失败",null);
		}
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}

	/**
	 * 列表展示(分页,条件模糊查询)
	 * @throws MyException 
	 */
	@PostMapping("/list")
	public ResponseBean list(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_列表展示");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String, Object> resultMap = upkeepService.getPageQuery(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 获取保养套餐分组下的时长
	 */
	@PostMapping("/getUpkeepTime")
	public ResponseBean getUpkeep_time(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "保养套餐_获取保养套餐分组下的时长");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		List<Map<String, Object>> resultMap = upkeepService.getUpkeep_timeByCondition(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}
}